package com.dq.controller.user;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.WxUserChangeQuery;
import com.dq.controller.in.request.ChangeWxUserOrgRequest;
import com.dq.controller.in.request.CustomerChangeRequest;
import com.dq.vo.Vo;

import com.dq.domain.org.Org;
import com.dq.domain.record.WxUserChangeRecord;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.service.AllService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author luchangtong
 * @Date 2020/1/2 14:04
 */
@RestController
@RequestMapping("/sys/wxUser/change")
@Api(description = "顾客变更记录")
public class SysWxUserChangeRecordController extends AllService {

    @ApiOperation("变更顾客技师")
    @PostMapping("changeWxUserStaff")
    public Vo changeWxUserStaff(@RequestBody CustomerChangeRequest customerChangeRequest) throws AdminException {
        StaffUser staffUser = dao.get(StaffUser.class, customerChangeRequest.getStaffUserId());
        if (staffUser == null) {
            throw new AdminException("转移目标技师不能为空");
        }
        List<WxUser> list = wxUserRepository.findByIdIn(customerChangeRequest.getWxUserId());
        HashSet<WxUserChangeRecord> set = new HashSet<>();
        list.forEach(wxUser -> {
            try {
                WxUserChangeRecord wxUserChangeRecord = staffUserChangeRecordService.save(wxUser, staffUser, null, WxUserChangeRecord.WxUserChangeType.technician);
                set.add(wxUserChangeRecord);
            } catch (AdminException e) {
                e.printStackTrace();
            }
        });
        wxUserRepository.saveAll(list);
        wxUserChangeRecordRepository.saveAll(set);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("变更顾客门店")
    @PostMapping("changeWxUserOrg")
    public Vo changeWxUserOrg(@RequestBody ChangeWxUserOrgRequest request) throws AdminException {
        StaffUser staffUser = dao.get(StaffUser.class, request.getStaffUserId());
        if (staffUser == null) {
            throw new AdminException("转移目标技师不能为空");
        }
        SysSection section = dao.get(SysSection.class, request.getOrgId());
        if (section == null) {
            throw new AdminException("变更的门店不能为空");
        }
        List<WxUser> list = wxUserRepository.findByIdIn(request.getWxUserId());
        HashSet<WxUserChangeRecord> set = new HashSet<>();
        list.forEach(wxUser -> {
            wxUser.setSection(section);
            try {
                WxUserChangeRecord wxUserChangeRecord = staffUserChangeRecordService.save(wxUser, staffUser, (Org) section, WxUserChangeRecord.WxUserChangeType.org);
                set.add(wxUserChangeRecord);
            } catch (AdminException e) {
                e.printStackTrace();
            }
        });
        wxUserRepository.saveAll(list);
        wxUserChangeRecordRepository.saveAll(set);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("顾客变更记录列表")
    @PostMapping("/get")
    public Vo get(@RequestBody WxUserChangeQuery query) {
        List<WxUserChangeRecord> records = wxUserChangeRecordService.findByWhere(query);
        Long total = wxUserChangeRecordService.countByWhere(query);
        List<WxUserChangeRecord.WxUserChangeRecordVo> list = new ArrayList<>();
        records.forEach(record -> {
            list.add(record.toStaffUserChangeRecordVo());
        });
        PageInfo pageInfo = new PageInfo(list);
        pageInfo.setTotal(total);
        return new Vo(pageInfo);
    }

    @ApiOperation("查看门店下的技师")
    @GetMapping("/findStaffByOrg/{id}")
    public Vo findStaffByOrg(@PathVariable Long id)throws AdminException{
        /*Org org = dao.get(Org.class,id);
        List<StaffUser> staffUserList = staffUserRepository.findByDeletedAndSectionAndStaffUserStatusAndStatus(false, org, StaffUser.StaffUserStatus.on, SysUser.Status.on);
        List<Map> maps = new ArrayList<>();
        staffUserList.forEach(staffUser -> {
            Map<String,Object> map = new HashMap<>();
            map.put("name",staffUser.getUserRealcName());
            map.put("id",staffUser.getId());
            maps.add(map);
        });
        return new Vo(maps);*/
        return null;
    }
}